package com.clothing.customization.service.impl;

import com.clothing.customization.common.PageResult;
import com.clothing.customization.common.exception.BusinessException;
import com.clothing.customization.entity.ClothingMaterial;
import com.clothing.customization.repository.ClothingMaterialRepository;
import com.clothing.customization.service.ClothingMaterialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * 服装材质服务实现类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ClothingMaterialServiceImpl implements ClothingMaterialService {
    
    private final ClothingMaterialRepository clothingMaterialRepository;
    
    @Override
    public PageResult<ClothingMaterial> getMaterialList(Integer pageNum, Integer pageSize, String name, Integer status) {
        // 创建分页对象，按创建时间倒序排序
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
        
        // 构建查询条件
        ClothingMaterial material = new ClothingMaterial();
        if (StringUtils.hasText(name)) {
            material.setName(name);
        }
        if (status != null) {
            material.setStatus(status);
        }
        
        // 创建匹配器，名称模糊匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                .withIgnoreNullValues();
        
        // 查询
        Page<ClothingMaterial> page = clothingMaterialRepository.findAll(Example.of(material, matcher), pageable);
        
        // 转换为自定义分页结果
        return new PageResult<>(page.getContent(), page.getTotalElements(), pageNum, pageSize);
    }
    
    @Override
    public ClothingMaterial getById(Long id) {
        return clothingMaterialRepository.findById(id)
                .orElseThrow(() -> new BusinessException("材质不存在"));
    }
    
    @Override
    @Transactional
    public ClothingMaterial addMaterial(ClothingMaterial material) {
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        material.setCreateTime(now);
        material.setUpdateTime(now);
        
        // 默认启用状态
        if (material.getStatus() == null) {
            material.setStatus(1);
        }
        
        return clothingMaterialRepository.save(material);
    }
    
    @Override
    @Transactional
    public ClothingMaterial updateMaterial(ClothingMaterial material) {
        // 检查材质是否存在
        ClothingMaterial existingMaterial = getById(material.getId());
        
        // 更新时间
        material.setUpdateTime(LocalDateTime.now());
        
        // 保留创建时间
        material.setCreateTime(existingMaterial.getCreateTime());
        
        return clothingMaterialRepository.save(material);
    }
    
    @Override
    @Transactional
    public boolean deleteMaterial(Long id) {
        // 检查材质是否存在
        ClothingMaterial material = getById(id);
        
        // 检查状态，只有禁用状态的材质才能删除
        if (material.getStatus() == 1) {
            throw new BusinessException("启用状态的材质不能删除，请先禁用");
        }
        
        clothingMaterialRepository.deleteById(id);
        return true;
    }
    
    @Override
    @Transactional
    public boolean updateStatus(Long id, Integer status) {
        // 检查材质是否存在
        ClothingMaterial material = getById(id);
        
        // 设置状态和更新时间
        material.setStatus(status);
        material.setUpdateTime(LocalDateTime.now());
        
        clothingMaterialRepository.save(material);
        return true;
    }
} 